using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;

using Elephant.Framework;

using Elephant.Utility;
using Elephant.Utility.Components;

namespace UsingMaterials
{
    [ComponentDependency(typeof(Transform), typeof(Mesh), typeof(LambertMaterial))]
    public class SphereRender : Drawable
    {
        LambertMaterial material;
        Transform transform;
        Camera camera;
        Mesh sphere;

        public override void Initialize(bool addDependencies)
        {
            base.Initialize(addDependencies);

            material = Owner.Find<LambertMaterial>();
            transform = Owner.Find<Transform>();
            sphere = Owner.Find<Mesh>();
        }

        public override void Start()
        {
            camera = Owner.Manager.FindWith<Camera>().Find<Camera>();

            sphere.AssetName = "Content\\Models\\sphere";
            // important to note that this has to be done after setting name (mesh needs to be loaded and stuff)
            sphere.Material = material;
        }

        public override void Draw(GameTime gameTime)
        {
            material.World =
                transform.World;
            material.WorldViewProjection =
                transform.World *
                camera.View *
                camera.Projection;
            material.ViewInverse =
                Matrix.Invert(camera.View);
            material.WorldInverse =
                Matrix.Invert(transform.World);

            // renderstates set

            // manually calling draw, to gain the advantage of being able to set/restore renderstates
            sphere.Draw(gameTime);

            // renderstates restore
        }
    }
}
